package com.xopencode.atomic;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Mcb
 * @date 2022/9/4
 * @apiNote 展示aba问题
 */
public class AbaTest {
        private final static AtomicInteger num = new AtomicInteger(100);

        public static void main(String[] args) {
            new Thread(() -> {
                num.compareAndSet(100, 101);
                num.compareAndSet(101, 100);
                System.out.println(Thread.currentThread().getName() + " 修改num之后的值：" + num.get());
            }).start();

            new Thread(() -> {
                try {
                    TimeUnit.SECONDS.sleep(3);
                    num.compareAndSet(100, 200);
                    System.out.println(Thread.currentThread().getName() + " 修改num之后的值：" + num.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
}
